﻿using SD.Common;
using SD.Infrastructure.EntityBase;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SlamDunk.MES.Domain.Entities.ConfigurationContext
{
    /// <summary>
    /// 用户扩展
    /// </summary>
    public class UserExtension : AggregateRootEntity
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected UserExtension()
        {
            //初始化导航属性
            this.RelatedTerminals = new HashSet<UserRelatedTerminal>();
            this.RelatedProcessSegments = new HashSet<UserRelatedProcessSegment>();
            this.RelatedStorageLocations = new HashSet<UserRelatedStorageLocation>();
        }
        #endregion

        #region 01.创建用户扩展构造器
        /// <summary>
        /// 创建用户扩展构造器
        /// </summary>
        /// <param name="loginId">用户账号</param>
        /// <param name="personId">人员Id</param>
        /// <param name="facilityId">工厂模型Id</param>
        public UserExtension(string loginId, Guid personId, Guid facilityId)
            : this()
        {
            base.Number = loginId;
            this.PersonId = personId;
            this.FacilityId = facilityId;
        }
        #endregion

        #endregion

        #region # 属性

        #region 人员Id —— Guid PersonId
        /// <summary>
        /// 人员Id
        /// </summary>
        public Guid PersonId { get; private set; }
        #endregion

        #region 工厂模型Id —— Guid FacilityId
        /// <summary>
        /// 工厂模型Id
        /// </summary>
        public Guid FacilityId { get; private set; }
        #endregion

        #region 导航属性 - 用户相关终端集 —— ICollection<UserRelatedTerminal> RelatedTerminals
        /// <summary>
        /// 导航属性 - 用户相关终端集
        /// </summary>
        public virtual ICollection<UserRelatedTerminal> RelatedTerminals { get; private set; }
        #endregion

        #region 导航属性 - 用户相关工艺段集 —— ICollection<UserRelatedProcessSegment> RelatedProcessSegments
        /// <summary>
        /// 导航属性 - 用户相关工艺段集
        /// </summary>
        public virtual ICollection<UserRelatedProcessSegment> RelatedProcessSegments { get; private set; }
        #endregion

        #region 导航属性 - 用户相关储存位置集 —— ICollection<UserRelatedStorageLocation> RelatedStorageLocations
        /// <summary>
        /// 导航属性 - 用户相关储存位置集
        /// </summary>
        public virtual ICollection<UserRelatedStorageLocation> RelatedStorageLocations { get; private set; }
        #endregion

        #endregion

        #region # 方法

        #region 修改用户扩展 —— void UpdateInfo( Guid personId, Guid facilityId)
        /// <summary>
        /// 修改用户扩展
        /// </summary>
        /// <param name="personId">人员Id</param>
        /// <param name="facilityId">工厂模型Id</param>
        public void UpdateInfo(Guid personId, Guid facilityId)
        {
            this.PersonId = personId;
            this.FacilityId = facilityId;
        }
        #endregion

        #region 关联终端 —— void RelateTerminals(IEnumerable<UserRelatedTerminal>...
        /// <summary>
        /// 关联终端
        /// </summary>
        /// <param name="relatedTerminals">用户相关终端集</param>
        public void RelateTerminals(IEnumerable<UserRelatedTerminal> relatedTerminals)
        {
            relatedTerminals = relatedTerminals?.ToArray() ?? new UserRelatedTerminal[0];

            this.ClearRelatedTerminals();
            if (relatedTerminals.Any())
            {
                this.RelatedTerminals.AddRange(relatedTerminals);
                foreach (UserRelatedTerminal relatedTerminal in relatedTerminals)
                {
                    relatedTerminal.UserExtension = this;
                }
            }
        }
        #endregion

        #region 关联工艺段 —— void RelateProcessSegments(IEnumerable<UserRelatedProcessSegment>...
        /// <summary>
        /// 关联工艺段
        /// </summary>
        /// <param name="relatedProcessSegments">用户相关工艺段集</param>
        public void RelateProcessSegments(IEnumerable<UserRelatedProcessSegment> relatedProcessSegments)
        {
            relatedProcessSegments = relatedProcessSegments?.ToArray() ?? new UserRelatedProcessSegment[0];

            this.ClearRelatedProcessSegments();
            if (relatedProcessSegments.Any())
            {
                this.RelatedProcessSegments.AddRange(relatedProcessSegments);
                foreach (UserRelatedProcessSegment relatedProcessSegment in relatedProcessSegments)
                {
                    relatedProcessSegment.UserExtension = this;
                }
            }
        }
        #endregion

        #region 关联储存位置 —— void RelateStorageLocations(IEnumerable<UserRelatedStorageLocation>...
        /// <summary>
        /// 关联储存位置
        /// </summary>
        /// <param name="relatedStorageLocations">用户相关储存位置集</param>
        public void RelateStorageLocations(IEnumerable<UserRelatedStorageLocation> relatedStorageLocations)
        {
            relatedStorageLocations = relatedStorageLocations?.ToArray() ?? new UserRelatedStorageLocation[0];

            this.ClearRelatedStorageLocations();
            if (relatedStorageLocations.Any())
            {
                this.RelatedStorageLocations.AddRange(relatedStorageLocations);
                foreach (UserRelatedStorageLocation relatedStorageLocation in relatedStorageLocations)
                {
                    relatedStorageLocation.UserExtension = this;
                }
            }
        }
        #endregion

        #region 清空用户相关终端 —— void ClearRelatedTerminals()
        /// <summary>
        /// 清空用户相关终端
        /// </summary>
        private void ClearRelatedTerminals()
        {
            foreach (UserRelatedTerminal relatedTerminal in this.RelatedTerminals.ToArray())
            {
                this.RelatedTerminals.Remove(relatedTerminal);
            }
        }
        #endregion

        #region 清空用户相关工艺段 —— void ClearRelatedProcessSegments()
        /// <summary>
        /// 清空用户相关工艺段
        /// </summary>
        private void ClearRelatedProcessSegments()
        {
            foreach (UserRelatedProcessSegment relatedProcessSegment in this.RelatedProcessSegments.ToArray())
            {
                this.RelatedProcessSegments.Remove(relatedProcessSegment);
            }
        }
        #endregion

        #region 清空用户相关储存位置 —— void ClearRelatedStorageLocations()
        /// <summary>
        /// 清空用户相关储存位置
        /// </summary>
        private void ClearRelatedStorageLocations()
        {
            foreach (UserRelatedStorageLocation relatedStorageLocation in this.RelatedStorageLocations.ToArray())
            {
                this.RelatedStorageLocations.Remove(relatedStorageLocation);
            }
        }
        #endregion

        #endregion
    }
}
